package team.southcat.wsyygh.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.southcat.wsyygh.common.result.Result;
import team.southcat.wsyygh.common.util.AdminInfoUtil;
import team.southcat.wsyygh.model.pojo.hospital.Department;
import team.southcat.wsyygh.model.pojo.hospital.DepartmentVo;
import team.southcat.wsyygh.hospital.service.DepartmentService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiuSiYi
 * @since 2023-04-20
 */
@Api(tags = "科室管理接口")
@RestController
@RequestMapping("/admin/hospital/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation("分页查询科室列表")
    @GetMapping("/getDepartmentPageList/{page}/{limit}")
    public Result getDepartmentPageList(@PathVariable int page, @PathVariable int limit, Department department, HttpServletRequest request){
        //map包括departments total
        String adminId = AdminInfoUtil.getAdminId(request);
        Page pageResult = departmentService.getDepartmentPageList(page,limit,department, adminId);
        return Result.ok(pageResult);
    }

    @ApiOperation("查询全部科室下的子科室")
    @GetMapping("/getParentList")
    public Result getParentList(HttpServletRequest request){
        String adminId = AdminInfoUtil.getAdminId(request);
        List<DepartmentVo> departments = departmentService.getAllByAdminId(adminId);
        return Result.ok(departments);
    }

    @ApiOperation("根据父科室查询科室")
    @GetMapping("/getChildList/{id}")
    public Result getChildList(@PathVariable long id){
        List<DepartmentVo> departments = departmentService.getByParentId(id);
        return Result.ok(departments);
    }


    @ApiOperation("根据id删除科室")
    @DeleteMapping("/deleteDepartmentById/{id}")
    public Result deleteDepartmentById(@PathVariable long id){
        Result result = departmentService.deleteById(id);
        return result;
    }

    @ApiOperation("批量删除科室")
    @DeleteMapping("/batchDeleteDepartmentByIds")
    public Result batchDeleteDepartmentByIds(@RequestBody List<Integer> ids){
        boolean flag = departmentService.deleteByIds(ids);
        return flag ? Result.ok() : Result.fail().message("删除失败！请检查删除项是否为父科室");
    }

    @ApiOperation("增加科室")
    @PostMapping("/addDepartment")
    public Result addDepartment(@RequestBody Department department, HttpServletRequest request){
        //map包括departments total
        String adminId = AdminInfoUtil.getAdminId(request);
        Result result = departmentService.addDepartment(department, adminId);
        return result;
    }

    @ApiOperation("根据id查询科室")
    @GetMapping("/getDepartmentById/{id}")
    public Result getDepartmentById(@PathVariable String id){
        Department department = departmentService.getById(id);
        return department != null ? Result.ok(department) : Result.fail().message("查询科室信息失败！");
    }

    @ApiOperation("修改科室数据")
    @PostMapping("/updateDepartment")
    public Result updateDepartment(@RequestBody Department department){
        boolean flag = departmentService.updateById(department);
        return flag ? Result.ok() : Result.fail().message("修改失败！");
    }

    @GetMapping("/getByName")
    public Result getByName(@RequestBody List<String> departmentName){
        List<Department> departments = departmentService.getByName(departmentName);
        return Result.ok(departments);
    }
}

